<?php
/**
 * 文件管理-月报文件（财务月报表，核算月报的基础数据来源）
 * (model)
 */
namespace App\Models\File;

use App\Func\UploadFile;
use Illuminate\Database\Eloquent\Model;
class FileProfitLT extends Model
{
    protected $table = 'file_profit_lt';

    protected $guarded = [];

    public static $_fileType = [
        1 => '1-销售明细费用',
        2 => '2-退货明细费用',
        3 => '3-第三方发货费',
        4 => '4-服务费',
        5 => '5-取消订单明细',
    ];

    public static $_status = [
        1 => '未解析',
        2 => '已解析',
    ];

    public static function fileDir($time){
        return 'profit'.DIRECTORY_SEPARATOR.'LT'.DIRECTORY_SEPARATOR.date('Ym',$time);
    }

    public static function fileName($fileType,$userId,$fileName){
        return $fileType.'-'.$userId.'-'.$fileName;
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            if($model->status == 2){
                throw new \Exception('文件已解析无法删除');
            }
            $uf = UploadFile::getInit();
            $fileDir = self::fileDir(strtotime($model->file_date));
            $fileName = self::fileName($model->file_type,$model->user_id,$model->file_name);
            $flag = $uf->delFile($fileDir.DIRECTORY_SEPARATOR.$fileName);
            if(!$flag){
                throw new \Exception('删除失败');
            }
        });
    }
}
